/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package deadline.statebutton.slice;

import com.deadline.statebutton.StateButton;
import deadline.statebutton.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * MainAbilitySlice
 *
 * @author wp
 * @since 2021-04-21
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x000110, "MainAbilitySlice");
    private static final int TEXT_SIZE = 40;
    private static final int DURATION_TIME = 2000;
    private static final int TOAST_DIALOG_RADIUS = 3000;
    private static final int DIALOG_PADDING = 20;
    private static final int STROKE_WIDTH = 10;
    private static final float WITH_DASH_PHASE = 5f;

    private static final float DIFF_RADII_TOP_LEFT_RADIUS = 0;
    private static final float DIFF_RADII_TOP_RIGHT_RADIUS = 20;
    private static final float DIFF_RADII_BOTTOM_RIGHT_RADIUS = 30;
    private static final float DIFF_RADII_BOTTOM_LEFT_RADIUS = 70;
    private static final float WITH_DASH_INTERVALS = 20;
    private static final int NUMBER3 = 3;
    private static final int NUMBER8 = 8;
    private static final int NUMBER16 = 16;
    private static final int ALPHA = 95;
    private ShapeElement diffRadiusElement;
    private ShapeElement withDashElement;
    private ShapeElement whitStrokeElement;
    private ShapeElement keepStrokeElement;
    private ShapeElement strokeBackgroundElement;
    private ShapeElement strokeBackgroundAnimationElement;
    private RgbColor white;

    // 扩展属性测试button
    private Button extendButton;

    // 改变字体颜色button
    private StateButton changeTextColor;

    // 改变背景颜色属性
    private StateButton changeBackGround;

    // 圆角
    private StateButton withRadius;
    private StateButton roundRadius;
    private StateButton differentRadius;
    private StateButton withDash;
    private StateButton withStroke;
    private StateButton keepStroke;
    private StateButton strokeBackground;
    private StateButton strokeBackgroundAnimation;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initButton();
    }

    // 初始化Button参数
    private void initButton() {
        float[] differentRadiiArray = new float[]{
            DIFF_RADII_TOP_LEFT_RADIUS, DIFF_RADII_TOP_LEFT_RADIUS,
            DIFF_RADII_TOP_RIGHT_RADIUS, DIFF_RADII_TOP_RIGHT_RADIUS,
            DIFF_RADII_BOTTOM_RIGHT_RADIUS, DIFF_RADII_BOTTOM_RIGHT_RADIUS,
            DIFF_RADII_BOTTOM_LEFT_RADIUS, DIFF_RADII_BOTTOM_LEFT_RADIUS};
        float[] withDashIntervals = new float[]{WITH_DASH_INTERVALS, WITH_DASH_INTERVALS,
            WITH_DASH_INTERVALS, WITH_DASH_INTERVALS};
        white = RgbColor.fromArgbInt(Color.WHITE.getValue());
        extendButton = (Button) findComponentById(ResourceTable.Id_extend_attribute);
        changeTextColor = (StateButton) findComponentById(ResourceTable.Id_change_text_color);
        changeBackGround = (StateButton) findComponentById(ResourceTable.Id_change_background_color);
        withRadius = (StateButton) findComponentById(ResourceTable.Id_with_radius);
        roundRadius = (StateButton) findComponentById(ResourceTable.Id_round_radius);

        differentRadius = (StateButton) findComponentById(ResourceTable.Id_different_radius);
        withDash = (StateButton) findComponentById(ResourceTable.Id_with_dash);
        withStroke = (StateButton) findComponentById(ResourceTable.Id_with_stroke);
        keepStroke = (StateButton) findComponentById(ResourceTable.Id_keep_stroke);
        strokeBackground = (StateButton) findComponentById(ResourceTable.Id_stroke_background);
        strokeBackgroundAnimation = (StateButton) findComponentById(ResourceTable.Id_stroke_background_animation);

        diffRadiusElement = (ShapeElement) differentRadius.getBackgroundElement();
        diffRadiusElement.setCornerRadiiArray(differentRadiiArray);
        differentRadius.setBackground(diffRadiusElement);

        withDashElement = (ShapeElement) withDash.getBackgroundElement();
        withDashElement.setDashPathEffectValues(withDashIntervals, WITH_DASH_PHASE);
        withDashElement.setStroke(STROKE_WIDTH, white);
        withDash.setBackground(withDashElement);

        whitStrokeElement = (ShapeElement) withStroke.getBackgroundElement();
        whitStrokeElement.setStroke(STROKE_WIDTH, white);
        withStroke.setBackground(whitStrokeElement);

        keepStrokeElement = (ShapeElement) keepStroke.getBackgroundElement();
        keepStrokeElement.setStroke(STROKE_WIDTH, white);
        keepStroke.setBackground(keepStrokeElement);

        strokeBackgroundElement = (ShapeElement) strokeBackground.getBackgroundElement();
        strokeBackgroundElement.setStroke(STROKE_WIDTH, white);
        strokeBackground.setBackground(strokeBackgroundElement);

        strokeBackgroundAnimationElement = (ShapeElement) strokeBackgroundAnimation.getBackgroundElement();
        strokeBackgroundAnimationElement.setStroke(STROKE_WIDTH, white);
        strokeBackgroundAnimation.setBackground(strokeBackgroundAnimationElement);

        // 设置点击事件监听
        extendButton.setClickedListener(this::onClick);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            // 扩展属性测试
            case ResourceTable.Id_extend_attribute:
                whenClick();
                break;
            default:
                HiLog.info(LABEL, "onClick");
        }
    }

    private void whenClick() {
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(new RgbColor(Color.BLACK.getValue() >> NUMBER16,
            Color.BLACK.getValue() >> NUMBER8, Color.BLACK.getValue() >> NUMBER3));
        shapeElement.setAlpha(ALPHA);
        DirectionalLayout directionalLayout = new DirectionalLayout(getContext());
        directionalLayout.setBackground(shapeElement);

        directionalLayout.setLayoutConfig(new ComponentContainer
            .LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT,
            ComponentContainer.LayoutConfig.MATCH_CONTENT));
        directionalLayout.setAlignment(LayoutAlignment.CENTER);
        Text dialog = new Text(getContext());
        dialog.setText("lalala");
        dialog.setTextColor(Color.WHITE);
        dialog.setTextAlignment(TextAlignment.HORIZONTAL_CENTER);
        dialog.setTextSize(TEXT_SIZE, Text.TextSizeType.PX);
        dialog.setPadding(DIALOG_PADDING, DIALOG_PADDING, DIALOG_PADDING, DIALOG_PADDING);
        directionalLayout.addComponent(dialog);
        new ToastDialog(getContext())
            .setSize(ComponentContainer.LayoutConfig.MATCH_CONTENT,
                ComponentContainer.LayoutConfig.MATCH_CONTENT)
            .setComponent(directionalLayout)
            .setDuration(DURATION_TIME)
            .setCornerRadius(TOAST_DIALOG_RADIUS)
            // Toast显示在界面底部
            .setAlignment(LayoutAlignment.BOTTOM)
            .show();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
